package me.zk.practice1.hw;

import java.util.LinkedList;

/**
 * @author zhangkai
 * @date 2023/8/4 15:21
 */
public class SB03 {

    public String helper(String s) {
        StringBuilder res = new StringBuilder();
        int multi = 0;
        LinkedList<Integer> stack_multi = new LinkedList<>();
        LinkedList<String> stack_res = new LinkedList<>();


        for (char c : s.toCharArray()) {
            if (c == '[') {
                stack_multi.addLast(multi);
                stack_res.addLast(res.toString());
                multi = 0;
                res = new StringBuilder();
            } else if (c == ']') {
                StringBuilder temp = new StringBuilder();
                int m = stack_multi.removeLast();
                for (int i = 0; i < m; i++) {
                    temp.append(res);
                }
                res = new StringBuilder(stack_res.removeLast() + temp);

            } else if (c >='0' && c <='9') {
                multi = multi*10 + Integer.parseInt(c+"");
            } else {
                res.append(c);
            }

        }

        return res.toString();

    }
}
